package leetcode.lcp.lcp20220924;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

//Solution2Test
public class Solution2 {

	public int transportationHub(int[][] path) {
		int m = path.length;
		Map<Integer, Integer> map = new HashMap<>();
		Set<Integer> set = new HashSet<>();
		Set<Integer> start = new HashSet<>();
		for (int i = 0; i < m; i++) {
			int a = path[i][0];
			int b = path[i][1];
			map.put(b, map.getOrDefault(b, 0) + 1);
			set.add(a);
			set.add(b);
			start.add(a);
		}
		int n = set.size();
		for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
			if (entry.getValue() == n - 1 && !start.contains(entry.getKey())) {
				return entry.getKey();
			}
		}
		return -1;
	}

}
